﻿using System.Collections.Generic;
using Entitas;
using System.Linq;
class EnemyRoamingMovementSystem : ReactiveSystem<CommandEntity>, ICleanupSystem
{
    CommandContext commandContext;
    EnemyContext enemyContext;
    public EnemyRoamingMovementSystem(Contexts contexts) : base(contexts.command)
    {
        commandContext = contexts.command;
        enemyContext = contexts.enemy;
    }



    protected override void Execute(List<CommandEntity> entities)
    {
        foreach (var e in entities)
        {

            var enemy = enemyContext.GetEntityWithName(e.roamingMovementCommand.Owner);
            var pos = enemy.position.value;
            var maxdistance = enemy.spawnPosition.value;
            var mindistance = enemy.spawnPosition.value;
            mindistance.x -= 2f;
            mindistance.y -= 2f;
            maxdistance.x += 2f;
            maxdistance.y += 2f;

            switch (e.roamingMovementCommand.direction)
            {
                case Direction.Up:
                    pos.y += enemy.speed.value;
                    pos.y = EntitasVec3.ClampVlaue(pos.y, mindistance.y, maxdistance.y);
                    enemy.ReplacePosition(pos);
                    enemy.ReplaceDirection(Direction.Up);
                    break;
                case Direction.Up_Left:
                    pos.y += enemy.speed.value;
                    pos.x -= enemy.speed.value;
                    pos.y = EntitasVec3.ClampVlaue(pos.y, mindistance.y, maxdistance.y);
                    pos.x = EntitasVec3.ClampVlaue(pos.x, mindistance.x, maxdistance.x);
                    enemy.ReplacePosition(pos);
                    enemy.ReplaceDirection(Direction.Up_Left);
                    break;
                case Direction.Up_Right:
                    pos.y += enemy.speed.value;
                    pos.x += enemy.speed.value;
                    pos.y = EntitasVec3.ClampVlaue(pos.y, mindistance.y, maxdistance.y);
                    pos.x = EntitasVec3.ClampVlaue(pos.x, mindistance.x, maxdistance.x);
                    enemy.ReplacePosition(pos);
                    enemy.ReplaceDirection(Direction.Up_Right);
                    break;
                case Direction.Down:
                    pos.y -= enemy.speed.value;
                    pos.y = EntitasVec3.ClampVlaue(pos.y, mindistance.y, maxdistance.y);
                    enemy.ReplacePosition(pos);
                    enemy.ReplaceDirection(Direction.Down);
                    break;
                case Direction.Down_Left:
                    pos.y -= enemy.speed.value;
                    pos.x -= enemy.speed.value;
                    pos.y = EntitasVec3.ClampVlaue(pos.y, mindistance.y, maxdistance.y);
                    pos.x = EntitasVec3.ClampVlaue(pos.x, mindistance.x, maxdistance.x);
                    enemy.ReplacePosition(pos);
                    enemy.ReplaceDirection(Direction.Down_Left);
                    break;
                case Direction.Down_Right:
                    pos.y -= enemy.speed.value;
                    pos.x += enemy.speed.value;
                    pos.y = EntitasVec3.ClampVlaue(pos.y, mindistance.y, maxdistance.y);
                    pos.x = EntitasVec3.ClampVlaue(pos.x, mindistance.x, maxdistance.x);
                    enemy.ReplacePosition(pos);
                    enemy.ReplaceDirection(Direction.Down_Right);
                    break;
                case Direction.Left:
                    pos.x -= enemy.speed.value;
                    pos.x = EntitasVec3.ClampVlaue(pos.x, mindistance.x, maxdistance.x);
                    enemy.ReplacePosition(pos);
                    enemy.ReplaceDirection(Direction.Left);
                    break;
                case Direction.Right:
                    pos.x += enemy.speed.value;
                    pos.x = EntitasVec3.ClampVlaue(pos.x, mindistance.x, maxdistance.x);
                    enemy.ReplacePosition(pos);
                    enemy.ReplaceDirection(Direction.Right);
                    break;
                default:
                    break;
            }
        }
    }

    protected override bool Filter(CommandEntity entity)
    {
        return entity.hasRoamingMovementCommand;
    }

    protected override ICollector<CommandEntity> GetTrigger(IContext<CommandEntity> context)
    {
        return context.CreateCollector(CommandMatcher.RoamingMovementCommand.Added());
    }
    public void Cleanup()
    {
        foreach (var e in commandContext.GetEntities().Where(x => x.hasRoamingMovementCommand))
        {
            e.Destroy();
        }

    }
}
